package com.skd.mall.order.controller;


import com.alibaba.cloud.commons.lang.StringUtils;
import com.skd.common.exception.NoStockExecption;
import com.skd.mall.order.config.AlipayTemplate;
import com.skd.mall.order.service.OrderService;
import com.skd.mall.order.vo.OrderConfirmVo;
import com.skd.mall.order.vo.OrderResponseVO;
import com.skd.mall.order.vo.OrderSubmitVO;
import com.skd.mall.order.vo.PayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class OrderWebController {

    @Autowired
    OrderService orderService;

    @Autowired
    private AlipayTemplate alipayTemplate;

    @RequestMapping("/toTrade")
    public String toTrade(Model model){
        OrderConfirmVo confirmVo = orderService.confirmOrder();
        model.addAttribute("confirmVo",confirmVo);
        return "confirm";
    }

    @RequestMapping("/orderSubmit")
    public String orderSubmit(OrderSubmitVO vo, Model model, RedirectAttributes redirectAttributes){
        OrderResponseVO responseVO = null;
        Integer code = 0;
        try{
            responseVO = orderService.submitOrder(vo);
            code = responseVO.getCode();
        }catch (NoStockExecption e){
            code = 2;
        }
        if (code == 0){
            model.addAttribute("orderResponseVO",responseVO);
            //下单成功
            return "pay";
        }else {
            String msg = "订单失败";
            if(code == 1){
                msg = msg + "：重复提交";
            }else if (code == 2){
                msg = msg + "：锁定库存失败";
            }
            redirectAttributes.addFlashAttribute("msg",msg);
            //下单失败
            return "redirect:http://order.skd.com/toTrade";
        }
    }

    @GetMapping("/orderPay/returnUrl")
    public String orderPay(@RequestParam(value = "orderSn",required = false) String orderSn,
                           @RequestParam(value = "out_trade_no",required = false) String out_trade_no){
        // TODO 完成相关的支付操作
        //System.out.println("orderSn = " + orderSn);
        //System.out.println("out_trade_no = " + out_trade_no);
        if(StringUtils.isNotBlank(orderSn)){
            orderService.handleOrderComplete(orderSn);
        }else{
            //orderService.updateOrderStatus(out_trade_no,OrderConstant.OrderStateEnum.TO_SEND_GOODS.getCode());
            orderService.handleOrderComplete(out_trade_no);
        }
        return "list";
    }

    /**
     * 支付
     * @param orderSn
     * @return
     */
    @GetMapping(value = "/payOrder",produces = "text/html")
    @ResponseBody
    public String orderPay(@RequestParam("orderSn") String orderSn){
        PayVo payVo = orderService.getorderPay(orderSn);
        String pay = alipayTemplate.pay(payVo);
        System.out.println("----------------------");
        return pay;
    }
}
